<template>
  <div class="bottom-bar">
    <check-button class="select-all"
                  :is-checked="isSelectAll"
                  @clickItem="checkClick"/>
    <span>全选</span>
    <span class="total-price">合计: ¥ {{ totalPrice }}</span>
    <div class="go-pay">
      <span>结算({{ $store.getters.cartLength }})</span>
    </div>
  </div>
</template>

<script>
import CheckButton from '@/views/cart/childComps/CheckButton'
import {mapGetters} from 'vuex'

export default {
  name: "BottomBar",
  components: {CheckButton},
  computed: {
    ...mapGetters({cartlist: 'cartListData'}),
    totalPrice() {
      return this.cartlist.filter(i => {
        return i.checked
      }).reduce((preValue, i) => {
        return preValue + i.count * i.newPrice
      }, 0).toFixed(2)
    },
    isSelectAll() {
      if (this.cartlist.length === 0) return false
      // 1. filter：根据计算长度决定
      // return !(this.cartlist.filter(item => !item.checked).length)

      // 2. find：根据查找元素决定
      return !this.cartlist.find(item => !item.checked)

      // 3. for：遍历查找
      /*  for (const item of this.cartlist) {
          if (!item.checked){
            return false
          }
        }
        return  true*/
    }
  },
  methods: {
    checkClick() {
      if (this.isSelectAll) { // 当全选时
        this.cartlist.forEach(item => item.checked = false)
      } else {  // 当部分或全部不选时
        this.cartlist.forEach(item => item.checked = true)
      }
    }
  }
}
</script>

<style scoped>
.bottom-bar {
  width: 100%;
  height: 40px;
  background-color: #eee;
  position: fixed;
  bottom: 50px;
  box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
  font-size: 14px;
  color: #888;
  line-height: 40px;
  padding-left: 35px;
}

.select-all {
  position: absolute;
  line-height: 0;
  left: 12px;
  top: 13px;
}

.total-price {
  margin-left: 15px;
  font-size: 16px;
  color: #666;
}

.go-pay {
  background-color: orangered;
  color: #fff;
  width: 30%;
  height: 100%;
  text-align: center;
  line-height: 40px;
  float: right;
}

</style>
